/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package its.mx.dsw2.view;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.Bl.CourseBLLocal;
import mx.its.dsw2.Bl.StudensBLLocal;
import mx.itsc.ds2.model.Course;
import mx.itsc.ds2.model.Student;

/**
 *
 * @author Martinez
 */
@ManagedBean
@SessionScoped
public class StudentBean {
    
    //--------------------- EJB's -----------------------------------//

    @EJB
    private StudensBLLocal studensBL;
    @EJB
    private CourseBLLocal courseBL;
    //---------------------------------------------------------------//
    
    //---------------------Variables---------------------------------//
    private List<Student> studentList;
    private List<Course> courseList;
    private Student student;
    private String action = "";
    private int SelectedCourse;
    //---------------------------------------------------------------//

    //---------------------Constructores-----------------------------//
    public StudentBean() {
        student = new Student();
    }
    //---------------------------------------------------------------//

    //----------------------Acciones---------------------------------//
    public String processAction() {
        if (action.equalsIgnoreCase("New")) {
            studensBL.register(student);
        } else if (action.equalsIgnoreCase("Edit")) {
            studensBL.modify(student);
        } else if (action.equalsIgnoreCase("Delete")) {
            studensBL.delete(student);
        }
        action = "";
        return "StudentList.xhtml";
    }

    public String processSelection(){
        student.setIdCourse(courseBL.findById(SelectedCourse));
    return "ConfirmStudent.xhtml";
    }
    public void prepareNew(ActionEvent evt) {
        action = "New";
        student = new Student();
    }

    public void prepareEdit(ActionEvent evt){
           action = "Edit";
//        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
//        student = studentList.get(index);
        courseList = courseBL.findByCourse(student);
    }
    
    public void prepareRemove(ActionEvent evt){
        action = "Delete";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        student = studentList.get(index);
        
        courseList = courseBL.findByCourse(student);
    }
    //---------------------------------------------------------------//
    
    //----------------------Getters y Setters------------------------//
    public List<Student> getStudentList() {
        studentList = studensBL.listAll();
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }
    
    public int getSelectedCourse() {
        return SelectedCourse;
    }

    public void setSelectedCourse(int SelectedCourse) {
        this.SelectedCourse = SelectedCourse;
    }
    
    public List<Course> getCourseList() {
        return courseList;
    }    
    //---------------------------------------------------------------//


}
